/**
 * Created by ZFL on 2017/9/22.
 */
var vm=new Vue({
    el:"#join",
    data:{
        selTerm:3,    //选择的期限   默认是3
        vipInfo:[],   //店主信息
    }
})
var bb=Number($.getStorage('returnWxpay'));
function PageInit(){
    getTerms();
    //如果不存在openid  去获取
    if(!$.getStorage('xmOpenid')){
        $.GetWXCode();
    }
}

function selectTerms(item){
    vm.selTerm=item.talentType;

}
function getTerms(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userId=0;
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        api:'yg_baijiaapp/talent/talentSalePrice',
        success:function(result){
            if(result.code==10000){
                var data=result.data;
                data.vipInfo.forEach(function(value,index){
                    value.money=divideNumber(value.money,100);
                    vm.vipInfo.unshift(value);
                })
            }else{
                YDUI.dialog.toast(result.message, 'error', 1000);
            }
        }
    })

}

//发送验证码
var $getCode = $('#send-code-id');
$getCode.sendCode({
    disClass: 'btn-disabled',
    secs: 60,
    run: false,
    runStr: '{%s}秒后重新获取',
    resetStr: '发送验证码'
});
function sendCode(){
    //YDUI.dialog.loading.open('发送中');
    $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.mobile = $('#phone-id').val();
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
            success: function (result) {
                //console.log(result);
                if (result.code == 10000) {
                    YDUI.dialog.toast('已发送', 'success', 1000);
                    $getCode.sendCode('start');
                } else if (result.code == 20112) {
                    YDUI.dialog.toast("短信发送失败",'error',function(){});
                } else if (result.code == 20000) {
                    YDUI.dialog.toast("请求失败",'error',function(){});
                } else if (result.code == 29901) {
                    YDUI.dialog.toast("无效参数",'error',function(){});
                } else {
                    YDUI.dialog.toast(result.message,'error',function(){});
                }

            }
        }
    );
}




//动态码登录
function bindPhone(){
    if($('#phone-id').val()==''){
        YDUI.dialog.toast('请输入手机号码','none',500);
    }else if($('#code-id').val()==''){
        YDUI.dialog.toast('请输入验证码','none',500);
    }else{
        $.ajaxHandler({
            model:function(){
                var obj={};
                obj.userName=$('#phone-id').val();
                obj.type=1;      //第三方登录类型   app不传
                obj.code=$('#code-id').val();     //验证码
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            approve: true,
            api: 'yg_baijiaapp/user/dynamicRegister',
            success: function (result) {
                //console.log(result);
                if (result.code == 10000) {
                    var data=result.data;
                    bb=1;
                    window.localStorage.setItem('returnWxpay',bb);
                    saveInfo(data);
                    Wxpay();

                } else {
                    YDUI.dialog.toast(result.message,1000);
                }
            }
        })
    }
}
//微信支付
function Wxpay(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.inviterUserId= $.getPara('inviterUserId');
            obj.openId=$.getStorage('xmOpenid');
            obj.userId=JSON.parse($.getStorage('xm_uData')).userId;
            obj.payType=3;
            obj.talentType=vm.selTerm;
            console.log(obj)
            return obj
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api: 'yg_baijiaapporder/wecha/weChaPayVip',
        success:function(result) {
            if (result.code ==10000) {
                var data = result.data;
                window.localStorage.setItem("data",data);
                if (data.paySign) {
                    onBridgeReady(data);
                } else {
                    //支付失败
                    _status = false;
                    YDUI.dialog.toast(result.message, 'error', 1000);
                }
            }else{

                YDUI.dialog.toast(result.message, 'error', 1000);
            }
        }
    })
}

var orderSn = '';   //订单号
//微信统一下单
function onBridgeReady(data){
    bb=0;
    window.localStorage.setItem('returnWxpay',bb);
    WeixinJSBridge.invoke('getBrandWCPayRequest', {
            "appId":""+data.appid+"",//公众号名称，由商户传入
            "timeStamp":""+data.timeStamp+"",//时间戳，自1970年以来的秒数
            "nonceStr":""+data.nonce_str+"", //随机串
            "package":"prepay_id="+data.prepay_id+"",
            "signType":"MD5",//微信签名方式：
            "paySign":""+data.paySign+"" //微信签名
        },
        function(res){
            // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回
            // ok，但并不保证它绝对可靠。
            //alert(res.err_msg);
            //console.log(res)
            //alert(res.err_msg);
            //$.delStorage('orderObject');
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                showBg();   //显示遮罩
            }else{

            }
        }
    );
}

//显示灰色 jQuery 遮罩层
function showBg() {
    var bh = $("body").height();
    var bw = $("body").width();
    $("#fullbg").css({
        height:bh,
        width:bw,
        display:"block",
    });
    $("#dialog").show()
    $('body').css("overflow","hidden")
    ;

}
//关闭灰色 jQuery 遮罩
function closeBg(){
    $("#fullbg,#dialog").hide();
    $('body').css("overflow","visible")
    location.href ='/b2c/app/downloadApp.html';
}
